#!/usr/bin/perl
# ourmodule3.pm 
package B::Tree;
use strict;

use GraphViz;
use B qw(main_root walkoptree_slow);
sub compile {
   return sub { 
      $g = new GraphViz;
      walkoptree_slow(main_root, "visit");
      print $g->as_dot;
   };
}
sub B::LISTOP::visit {
   my $self = shift;
   $g->add_node({name => $$self, label => $self->name});
   my $node = $self->first;
   while ($$node != ${$self->last}) {
      $g->add_edge({from => $$self, to => $$node});
      $node = $node->sibling;
   }
   $g->add_edge({from => $$self, to => $$node});
}
sub B::BINOP::visit {
   my $self = shift;
   my $first = $self->first;
   my $last = $self->last;
   $g->add_node({name => $$self, label => $self->name});
   $g->add_edge({from => $$self, to => $$first});
   $g->add_edge({from => $$self, to => $$last});
}
sub B::UNOP::visit {
   my $self = shift;
   my $first = $self->first;
   $g->add_node({name => $$self, label => $self->name});
   $g->add_edge({from => $$self, to => $$first});
}
sub B::OP::visit {
   my $self = shift;
   $g->add_node({name => $$self, label => $self->name});
}
1;
